package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.application.Constant;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.QRCodeUtil;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-17.
 * 扫码支付
 */

public class ScanCodePayActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_back;
    private TextView tv_goodName;
    private TextView tv_goodType;
    private TextView tv_goodNum;
    private TextView tv_goodTemp;
    private TextView tv_goodPrice;
    private ImageView img_aliCode;
    private ImageView img_wxingCode;

    private String orderurl;
    private int runTime = 3 * 1000;
    private boolean mIsHeat = false;
    private boolean isPause = false;
    private Handler myHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (!isPause) {
//                getProductByCode("0001");
                machineWs();
                myHandler.postDelayed(mRunnable, 2000);
            }
//            intent();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scancodepay_layout);
        initView();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        tv_goodName = findViewById(R.id.tv_goodName);
        tv_goodType = findViewById(R.id.tv_goodType);
        tv_goodNum = findViewById(R.id.tv_goodNum);
        tv_goodTemp = findViewById(R.id.tv_goodTemp);
        tv_goodPrice = findViewById(R.id.tv_goodPrice);
        img_aliCode = findViewById(R.id.img_aliCode);
        img_wxingCode = findViewById(R.id.img_wxingCode);
        btn_back.setOnClickListener(this);

        int milkType = getIntent().getIntExtra(Constant.MILK_TYPE, 0);
        String spec = getIntent().getStringExtra("spec");
        int milkNum = getIntent().getIntExtra("milkNum", 0);
        String mMilkPrice = getIntent().getStringExtra("milkPrice");
        mIsHeat = getIntent().getBooleanExtra("mIsHeat", false);
        orderurl = getIntent().getStringExtra("orderurl");
        tv_goodName.setText(milkType == 1 ? "酸奶" : "鲜奶");
        tv_goodType.setText(spec);
        tv_goodNum.setText(milkNum + "瓶");
        tv_goodTemp.setText(mIsHeat ? "加热" : "不加热");
        tv_goodPrice.setText("￥" + mMilkPrice);
//        img_aliCode.setImageBitmap(QRCodeUtil.createQRCode(220,220,orderurl));
        Bitmap bitLoge = BitmapFactory.decodeResource(getResources(), R.drawable.img_wxing_logo);
        img_aliCode.setImageBitmap(QRCodeUtil.createQRImage(orderurl, 180, 180, null));

        myHandler.postDelayed(mRunnable, 5000);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void intent(String strData) {
        Intent intent = new Intent(ScanCodePayActivity.this, PaySuccessActivity.class);
        intent.putExtra("isHeat", mIsHeat);
        intent.putExtra("strTakeData", strData);

        startActivity(intent);
        finish();
    }

    private void machineWs() {
        Call<String> call = MilkApi.getInstance().machineWs(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                isPause = true;
                intent(data);
            }

            @Override
            public void onError(int code, String error) {
//                mToast(error);
            }
        });

    }

    //是否出货
    private void getProductByCode(String sn) {
        Call<String> call = MilkApi.getInstance().getProductByCode(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                isPause = true;
//                intent();
            }

            @Override
            public void onError(int code, String error) {
//                mToast(error);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        isPause = true;
        myHandler.removeCallbacks(mRunnable);

    }
}
